package com.tianbing.adlottery.controller.user;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jubao.ejb.business.StoreBusiness;
import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.entity.CouponItem;
import com.jubao.ejb.entity.UserWebsite;
import com.jubao.ejb.entity.Website;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class MySpaceController extends MultiActionController {
	Logger logger = Logger.getLogger(MySpaceController.class.getName());

	private String mySpaceView;

	private String jsonView;

	private String listMyCouponItemView;

	private String listFavoriteWebSiteView;

	private String deleteFavoriteWebSiteSuccessView;

	private String ajaxAutoLoginSuccView;

	private UserBusiness userBusiness;

	private StoreBusiness storeBusiness;

	private SkinUtils skinUtils;

	public ModelAndView showMySpace(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(mySpaceView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		return mv;
	}

	public ModelAndView listFavoriteWebsite(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listFavoriteWebSiteView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		List<UserWebsite> userWebsiteList = userBusiness.getFavoriteWebSiteList(auth.getUserSysId(), pgno, pgsize);
		long totalCount = userBusiness.getFavoriteWebSiteCount(auth.getUserSysId());
		model.put("userWebsiteList", userWebsiteList);

		int pageItemNum = userWebsiteList == null ? 0 : userWebsiteList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public ModelAndView listMyCouponItem(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listMyCouponItemView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		List<CouponItem> couponItemList = storeBusiness.getCouponItemList(-1,-1,auth.getUserSysId(), pgno, pgsize);
		long totalCount = storeBusiness.getCouponItemCount(-1,-1,auth.getUserSysId());
		model.put("couponItemList", couponItemList);

		int pageItemNum = couponItemList == null ? 0 : couponItemList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public ModelAndView addonAjaxGetUserFavoriteWebsites(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(jsonView);
		Map model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		List<UserWebsite> userWebsites = userBusiness.getFavoriteWebSiteList(auth.getUserSysId(), 0, 100);
		//$ret['site'][] = array($site['sid'],$site['uri'],$site['name'],$site["searchpath"],$site["rsspath"],$site['favoriteranking'],$site["ranking"]);
		JSONObject jsonObj = new JSONObject();
		if (userWebsites != null) {
			JSONArray jsonWebsiteList = new JSONArray();
			for (int i = 0; i < userWebsites.size(); i++) {
				UserWebsite uw = userWebsites.get(i);
				JSONArray jsonList = new JSONArray();
				Website w = uw.getWebsite();
				jsonList.add(w.getSysId());
				jsonList.add(w.getId());
				jsonList.add(w.getName()==null?"":w.getName());
				jsonList.add(w.getSearchUrl()==null?"":w.getSearchUrl());
				jsonList.add(w.getRssUrl()==null?"":w.getRssUrl());				
				jsonList.add(uw.getRank()==null?0:uw.getRank());
				jsonList.add(w.getRank()==null?0:w.getRank());
				jsonWebsiteList.add(jsonList);
			}
			jsonObj.put("site", jsonWebsiteList);
		}
		model.put("json_string", jsonObj.toString());
		return mv;
	}

	public ModelAndView deleteFavoriteWebSite(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(deleteFavoriteWebSiteSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long wbsid = ServletRequestUtils.getLongParameter(request, "wbsid", -1);
		if (wbsid > 0) {
			userBusiness.deleteUserFavoriteWebSite(auth.getUserSysId(), wbsid);
		}

		return mv;
	}

	public ModelAndView ajaxAutoLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(ajaxAutoLoginSuccView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		skinUtils.getLastVisitedHorde(request, response);
		skinUtils.setHasCheckedAutoLogin(request, response);
		return mv;
	}

	/**
	 * @param logger the logger to set
	 */
	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	/**
	 * @param mySpaceView the mySpaceView to set
	 */
	public void setMySpaceView(String mySpaceView) {
		this.mySpaceView = mySpaceView;
	}

	/**
	 * @param listFavoriteWebSiteView the listFavoriteWebSiteView to set
	 */
	public void setListFavoriteWebSiteView(String listFavoriteWebSiteView) {
		this.listFavoriteWebSiteView = listFavoriteWebSiteView;
	}

	/**
	 * @param deleteFavoriteWebSiteSuccessView the deleteFavoriteWebSiteSuccessView to set
	 */
	public void setDeleteFavoriteWebSiteSuccessView(String deleteFavoriteWebSiteSuccessView) {
		this.deleteFavoriteWebSiteSuccessView = deleteFavoriteWebSiteSuccessView;
	}

	/**
	 * @param ajaxAutoLoginSuccView the ajaxAutoLoginSuccView to set
	 */
	public void setAjaxAutoLoginSuccView(String ajaxAutoLoginSuccView) {
		this.ajaxAutoLoginSuccView = ajaxAutoLoginSuccView;
	}

	/**
	 * @param userBusiness the userBusiness to set
	 */
	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	/**
	 * @param skinUtils the skinUtils to set
	 */
	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setJsonView(String jsonView) {
		this.jsonView = jsonView;
	}

	public void setListMyCouponItemView(String listMyCouponItemView) {
		this.listMyCouponItemView = listMyCouponItemView;
	}

	public void setStoreBusiness(StoreBusiness storeBusiness) {
		this.storeBusiness = storeBusiness;
	}

}
